package com.avocado.bt.abs.composite;

import com.avocado.bt.IBTContext;
import com.avocado.bt.IBTNode;
import com.avocado.bt.abs.BaseNode;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Author: zsh
 * @Date: 2023/10/16 16:57
 */
public abstract class CompositeNode extends BaseNode {

    private final List<IBTNode> childNodes = new ArrayList<>();

    public CompositeNode(String param) {
        super(param);
    }

    @Override
    public void addChildNode(IBTNode node) {
        this.childNodes.add(node);
    }

    public boolean hasChildNode(IBTNode node) {
        return this.childNodes.contains(node);
    }

    public void removeChildNode(IBTNode node) {
        this.childNodes.remove(node);
    }

    public void clearChildNodes() {
        this.childNodes.clear();
    }

    public Collection<IBTNode> getChildNodes() {
        return childNodes;
    }

    @Override
    public void terminate(IBTContext context) {
        for (IBTNode childNode : getChildNodes()) {
            childNode.setState(null);
        }
    }

}
